# 序列化
import json

id = 1
name = 'tom'

dict = {'id': id, 'name': name}
# json_str = json.dumps(dict)
json_str = json.dumps(dict, indent=2,ensure_ascii=False)
# json_str = json.dumps(dict, indent=4,ensure_ascii=False)
print(json_str)



number = 1234.5678
name = 'tom'
print("Number is {}".format(number))  # 1.format的位置参数用法
print("Number is {}, and your name is {}".format(number,name))  #format的位置参数用法

name = 'lzy'
age = 22

# 2.format的关键字参数用法
messages = "My name is {name},my age is {age}".format(name=name, age=age)
print(messages)
# 3.format的格式选项用法，显示小数点后零位
print("Number is {:.2f}".format(number))

# 4.对齐和填充
# 输出："Name: John"。在这个例子中，{: > 10} 表示将字符串右对齐，并在左侧填充空格，总宽度为10
name = "John"
formatted_name = "Name: {:>10}".format(name)
print(formatted_name)

# 5.格式化类型
# format方法支持不同的格式化类型，例如整数、浮点数、十六进制等
number = 42
formatted_number = "Formatted number: {:b}".format(number)
print(formatted_number)

number = 255

# 不同进制
print(f"十进制：{number:d}")  # 255
print(f"二进制：{number:b}")  # 11111111
print(f"八进制：{number:o}")  # 377
print(f"十六进制：{number:x}")  # ff (小写)
print(f"十六进制：{number:X}")  # FF (大写)

# 浮点数
pi = 3.1415926
print(f"保留2位小数：{pi:.2f}")  # 3.14
print(f"百分比：{0.75:.1%}")  # 75.0%

# 对齐和填充
text = "Python"
print(f"右对齐：{text:>10}")  # '    Python'
print(f"左对齐：{text:<10}")  # 'Python    '
print(f"居中对齐：{text:^10}")  # '  Python  '
print(f"填充：{text:*^10}")  # '**Python**'

# 数字格式化
num = 1234.5678
print(f"千分位：{num:,}")  # 1,234.5678
print(f"科学计数法：{num:.2e}")  # 1.23e+03

data1 = 123456
date2 = "json:{}".format(data1)
print(date2)
print("id:",data1)


# id = 1
# name = Amy
# 通过format转换为json格式，比如下面的形式print

# {
#     id：1,
#     name: tom
# }

id = 1
name = "Amy"

# 使用format方法
json_str = """
{{
    "id": {},
    "name": "{}"
}}
""".format(id, name)

print(json_str)